package com.example.customer01.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.customer01.common.ApiResponse;
import com.example.customer01.pojo.StudentCourse;
import com.example.customer01.service.StudentCourseService;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 學生選課控制器
 */
@RestController
@RequestMapping("/api/student-course")
public class StudentCourseController {

    @Resource
    private StudentCourseService studentCourseService;

    /**
     * 分頁查詢選課記錄
     */
    @GetMapping("/list")
    public ResponseEntity<IPage<StudentCourse>> getStudentCoursePage(
            @RequestParam(defaultValue = "1") Integer pageNum,
            @RequestParam(defaultValue = "10") Integer pageSize,
            @RequestParam(required = false) String keyword) {
        Page<StudentCourse> page = new Page<>(pageNum, pageSize);
        return ResponseEntity.ok(studentCourseService.getStudentCoursePage(page, keyword));
    }

    /**
     * 新增選課記錄
     */
    @PostMapping("/add")
    public ResponseEntity<StudentCourse> createStudentCourse(@RequestBody StudentCourse studentCourse) {
        return ResponseEntity.ok(studentCourseService.createStudentCourse(studentCourse));
    }

    /**
     * 批量創建選課記錄
     */
    @PostMapping("/batch")
    public ApiResponse<List<StudentCourse>> batchCreate(@RequestBody List<StudentCourse> studentCourses) {
        try {
            List<StudentCourse> result = studentCourseService.batchCreateStudentCourses(studentCourses);
            return ApiResponse.success(result);
        } catch (Exception e) {
            return ApiResponse.error(e.getMessage());
        }
    }

    /**
     * 刪除選課記錄
     */
    @DeleteMapping("/{id}")
    public ApiResponse<Void> delete(@PathVariable Integer id) {
        try {
            studentCourseService.deleteStudentCourse(id);
            return ApiResponse.success();
        } catch (Exception e) {
            return ApiResponse.error(e.getMessage());
        }
    }

    /**
     * 獲取學生的選課列表
     */
    @GetMapping("/student/{studentId}")
    public ApiResponse<List<StudentCourse>> getStudentCourses(@PathVariable Integer studentId) {
        try {
            List<StudentCourse> courses = studentCourseService.getStudentCourses(studentId);
            return ApiResponse.success(courses);
        } catch (Exception e) {
            return ApiResponse.error(e.getMessage());
        }
    }

    /**
     * 獲取課程的學生列表
     */
    @GetMapping("/course/{courseId}")
    public ResponseEntity<List<StudentCourse>> getCourseStudents(@PathVariable Integer courseId) {
        return ResponseEntity.ok(studentCourseService.getCourseStudents(courseId));
    }

    /**
     * 檢查學生是否已選修課程
     */
    @GetMapping("/check/{studentId}/{courseId}")
    public ResponseEntity<Boolean> checkStudentCourse(
            @PathVariable Integer studentId,
            @PathVariable Integer courseId) {
        return ResponseEntity.ok(studentCourseService.checkStudentCourse(studentId, courseId));
    }

    /**
     * 查詢學生的選課詳情（包含課程信息）
     */
    @GetMapping("/details/{studentId}")
    public ApiResponse<List<StudentCourse>> getStudentCoursesWithDetails(@PathVariable Integer studentId) {
        try {
            List<StudentCourse> details = studentCourseService.getStudentCoursesWithDetails(studentId);
            return ApiResponse.success(details);
        } catch (Exception e) {
            return ApiResponse.error(e.getMessage());
        }
    }
} 